package com.example.controller;

import cn.hutool.core.date.DateUtil;
import com.example.annotation.PassToken;
import com.example.entity.RedisLockDTO;
import com.example.entity.Staff;
import com.example.entity.User;
import com.example.entity.persion.Persion;
import com.example.feign.FetClinet;
import com.example.feign.domin.dto.JdOrderDTO;
import com.example.service.IUserService;
import com.example.utils.ListUtil;
import com.example.utils.RedisUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;

/**
 * Create By ChenXiAoBin
 * on 2019/1/18
 */

@RestController
@RequestMapping("/V1.0")
public class UserController {
    private Logger LOG = Logger.getLogger(String.valueOf(this.getClass()));

    @Autowired
    private IUserService userService;

    @Autowired
    private FetClinet fetClinet;


    @GetMapping("getUser/{id}")
    @ApiOperation(value = "ID存reids 返回uuid", notes = "ID存热reids 返回uuid")
    @ApiImplicitParam(name = "id", value = "ID", paramType = "path", required = true, dataType = "String")
    public String getUser(@PathVariable("id") String id) {
        LOG.info("log测试");
        String b = UUID.randomUUID().toString();
        RedisUtil.set(b, id);
        return b;
    }


    @GetMapping(value = "/get/encry")
    @PassToken
    public void getEncry() {
        fetClinet.getEncry();
    }

    @GetMapping("")
    @ApiOperation(value = "测试查询redis", notes = "测试查询redis")
    public String findUser(HttpServletRequest request) {
        LOG.info("reids取值");
        String f = request.getHeader("Authorization");
        return RedisUtil.get(f);
    }


//    @PutMapping("/{type}")
//    @ApiOperation(value = "测试redis工具类", notes = "测试redis工具类")
//    public void redisTemplate(@ApiParam(required = true, name = "type", value = "入参")
//                              @PathVariable("type") String type) {
//        RedisOperator redisOperator = new RedisOperator();
//        redisOperator.set(type, "陈晓斌", 20);
//    }


    @PostMapping("/redis_lock")
    @PassToken
    @ApiOperation(value = "测试redis锁 ", notes = "测试redis锁")
    public void redisLock(@RequestBody RedisLockDTO redisLock) {

    }

    @PutMapping("/cut/word")
    @PassToken
    @ApiOperation(value = "单词切割以及集合数组之间的转换", notes = "单词切割")
    public void map() {
//        String splitWord = "https://res.pandateacher.com/CLeWOrD_cLeH0JEAXequeUCudlEwOrD_L-9_画板%201%20副本%203.png?auto-uploaded=b88agu4zc0c";
//        String nextWord = "https://res.pandateacher.com/CLeWOrD_cLeH0JEAXequeUCudlEwOrD_L-9_画板%201%20副本%203.png";
//        String[] split = splitWord.split("\\?");
//        List<String> stringList = Arrays.asList(nextWord.split("\\?"));
//
//        //集合转数组
//        List <Integer> arrs = new ArrayList<>();
//        arrs.add(1);
//        arrs.add(2);
//        arrs.add(3);
//        Integer[] arr = arrs.toArray(new Integer[0]);
//        System.out.println(arr);
//        System.out.println(Arrays.toString(split));
        String name = "你好";
        List<String> stringList = Arrays.asList(name.split("\\|"));

    }


    @PostMapping("/post")
    @ApiOperation(value = "post请求", notes = "post请求")
    public Persion post(HttpServletRequest request, @RequestBody Persion persion) {
        LOG.info("post请求");
        LOG.info(request.getRemoteAddr());
        LOG.info(request.getQueryString());
        return persion;

    }


    @PostMapping("/list/test")
    @ApiOperation(value = "list车上", notes = "list车上")
    @PassToken
    public void listtest() {
        List<Integer> id1 = new ArrayList<>();
        id1.add(1);
        id1.add(2);
        id1.add(3);

        List<Integer> id2 = new ArrayList<>();
        id2.add(2);
        List<Integer> id = ListUtil.removeRepeat2(id1, id2);
    }

    @PutMapping("/stream")
    @ApiOperation(value = "stream流遍历", notes = "stream流遍历")
    @PassToken
    public void streams() {

        List<Double> list = DoubleStream.of(1.0, 2.0, 3.0)
                .collect(ArrayList<Double>::new, ArrayList::add, ArrayList::addAll);


        List<Staff> staff = new ArrayList<>();
        Staff staffOne = new Staff();
        staffOne.setName("陈晓斌");
        staffOne.setAge(18);
//        staffOne.setTime(new Date());

        Staff staffSecond = new Staff();
        staffSecond.setName("陈晓斌");
        staffSecond.setAge(123);
//        staffSecond.setTime(DateUtil.offsetDay(new Date(), 1));

        Staff staffThread = new Staff();
        staffThread.setName("陈晓");
        staffThread.setAge(1);
        staffThread.setTime(DateUtil.offsetDay(new Date(), 2));

        staff.add(staffOne);
        staff.add(staffSecond);
        staff.add(staffThread);

        Optional<Staff> loginEnterprise = staff.stream().max(Comparator.comparing(Staff::getTime));

        Staff staff1 = loginEnterprise.get();


        List<String> collect = staff.stream().map(x -> x.getName()).collect(Collectors.toList());
        String[] staffName = staff.stream().map(x -> x.getName()).toArray(String[]::new);
        String [] name = (String[]) staff.stream().map(x -> x.getName()).toArray();

        //过滤出集合中字段为指定的数据
        List<Staff> staffList = staff.stream().filter(str -> str.getName().equals("陈晓斌")).collect(Collectors.toList());

        //获取某个字段的集合
        List<String> id = new ArrayList<>();
        staff.stream().forEach(str ->
            id.add(str.getName())
        );

        List<Staff> unique = staff.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Staff::getName))), ArrayList::new)
        );

    }

    @PutMapping("/hast/set")
    @ApiOperation(value = "hastset", notes = "hastset")
    public void hashSet() {
        JdOrderDTO jdOrderDTO = new JdOrderDTO();
        jdOrderDTO.setApikey("a236011d8c43e405");
        jdOrderDTO.setTime("2019100814");
//        jdClinet.getOrder(jdOrderDTO);
    }

    @PutMapping("/string/util")
    public void StringUtil() {
        String wechatAccountIds = "12,13,14,15,16";

        List<Integer> wechatAccountIdList = Arrays.stream(wechatAccountIds.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        LOG.info(";;;;;");
    }


//    @ApiOperation(value = "群自动回复时维护", notes = "群自动回复时维护")
//    @PostMapping("/set/all/cicle")
//    public String setAllCicle(@Valid @RequestBody AddCicleSetDTO addCicleSetDTO) {
//        redisTemplateUtils.set("cicle_of_friend_" + UUID.randomUUID(), addCicleSetDTO.getValue());
//        return "cicle_of_friend_" + UUID.randomUUID();
//    }

    private User test() {
        User user = new User();
        return user;
    }

    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }
}
